package com.wind.plugin.annotator;

import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.wind.plugin.common.Common.*;
import static com.wind.plugin.common.Constant.GIT_DATE;
import static com.wind.plugin.window.Utils.annotateRange;
import static com.wind.plugin.window.Utils.highlightRange;

public class JavaAnnotator implements Annotator {

    private static final Pattern CUR_PATTERN = Pattern.compile("\\b(CUR|curr|" +
            "curCode|currenyCode|eCode|NBCBCURCODE|CURRENCY_ID|actcur)\\b", Pattern.CASE_INSENSITIVE);

    @Override
    public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
        if (element instanceof PsiFile) {
            // 使用 Visitor 模式精准处理标识符
            element.accept(new CurrencyIdentifierVisitor(holder));
        }
    }
}

// 新增内部 Visitor 类
class CurrencyIdentifierVisitor extends JavaRecursiveElementWalkingVisitor {
    private final AnnotationHolder holder;

    public CurrencyIdentifierVisitor(AnnotationHolder holder) {
        this.holder = holder;
    }

    @Override
    public void visitIdentifier(PsiIdentifier identifier) {
        // 精确匹配标识符
        String text = identifier.getText();
        Matcher matcher = getCurPattern().matcher(text);

        TextRange range = identifier.getTextRange();
        while (matcher.find()) {
            TextRange targetRange = new TextRange(
                    range.getStartOffset() + matcher.start(),
                    range.getStartOffset() + matcher.end()
            );
            annotateRange(targetRange.getStartOffset(), targetRange.getEndOffset(),
                        holder, "标记项请注意");

        }
        super.visitIdentifier(identifier); // 继续遍历子元素
    }
}

/**
 * // 检查是否是字符串文本表达式
 *         if (element instanceof PsiIdentifier) {
 *             PsiIdentifier identifier = (PsiIdentifier) element;
 *             String text = identifier.getText();
 *             Matcher matcher = getCurPattern().matcher(text);
 *             // 直接使用当前元素的文本范围（修复范围计算问题）
 *             TextRange range = identifier.getTextRange();
 *             while (matcher.find()) {
 *                 //PsiFile containingFile = element.getContainingFile();
 *                 //Document document = containingFile.getViewProvider().getDocument();
 *                 //assert document != null;
 *                 //String filePath = "";
 *                 //PsiElement psiFile = PsiTreeUtil.getParentOfType(element, PsiElement.class);
 *                 //if (psiFile != null) {
 *                 //    filePath = psiFile.getContainingFile().getVirtualFile().getPath();
 *                 //}
 *
 *                 int start = range.getStartOffset() + matcher.start();
 *                 int end = range.getStartOffset() + matcher.end();
 *
 *                 //int lineNumber = document.getLineNumber(end) + 1;
 *
 *                 // 获取当前元素所在项目的 Git 仓库路径
 *                 //String gitRepoPath = getGitRepositoryPath(element.getProject());
 *                 //boolean shouldAnnotate;
 *                 // try{
 *                     //shouldAnnotate = isCommitAfterDate(gitRepoPath, filePath, lineNumber, GIT_DATE);
 *                 //} catch (Exception e) {
 *                  //   shouldAnnotate = true;
 *                 //}
 *
 *                 //if (shouldAnnotate) {
 *                         // 1.注释高亮
 *                         annotateRange(start, end, holder, "标记项请注意");
 *                         // 2.mark高亮
 *                         //highlightRange(element, holder, "币种项请注意", start, end);
 *                 //}
 *             }
 *         }
 */

